package com.wxl.common.util

import android.app.Activity
import android.graphics.Bitmap
import android.view.ViewTreeObserver
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.wxl.common.AppContext
import com.wxl.common.xbanner.RoundCorners

/**
 * create file time : 2021/3/1
 * create user : wxl
 * subscribe :
 */
class ImgLoader {

    companion object {


        private fun imageViewContextDestroyed(imageView: ImageView): Boolean {
            if (imageView.context is Activity) {
                return (imageView.context as Activity).isDestroyed
            }
            return false
        }

        /**
         * 加载资源图片
         */
        fun loadResouce(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(RequestOptions.centerCropTransform())
                .into(imageView)
        }


        /**
         * 加载资源图片
         */
        fun loadResouce(imageView: ImageView, resId: Int, error: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId)
                .apply(RequestOptions.centerCropTransform().error(error).placeholder(error))
                .into(imageView)
        }

        /**
         * 加载资源图片
         */
        fun loadScaledWHResource(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).asBitmap().load(resId)
                .apply(RequestOptions.centerCropTransform())
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        imageView.viewTreeObserver.addOnGlobalLayoutListener(object :
                            ViewTreeObserver.OnGlobalLayoutListener {
                            override fun onGlobalLayout() {
                                imageView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                                val c = resource.height.toFloat() / resource.width.toFloat()
                                imageView.layoutParams.height = (c * imageView.width).toInt()
                                Glide.with(imageView)
                                    .load(resource)
                                    .apply(RequestOptions.centerCropTransform())
                                    .into(imageView)
                            }

                        })
                    }

                })
        }

        /**
         * 加载资源图片
         */
        fun loadCircleCropResouce(imageView: ImageView, resId: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(RequestOptions.circleCropTransform())
                .into(imageView)
        }

        /**
         * 加载资源图片
         */
        fun loadCornerResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(corner, corner, corner, corner)
                )
            ).into(imageView)
        }


        /**
         * 加载资源图片
         */
        fun loadCornerTopResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(corner, corner, 0f, 0f)
                )
            ).into(imageView)
        }

        /**
         * 加载资源图片
         */
        fun loadCornerLeftResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(corner, 0f, 0f, corner)
                )
            ).into(imageView)
        }

        /**
         * 加载资源图片
         */
        fun loadCornerRightResouce(imageView: ImageView, resId: Int, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(resId).apply(
                RequestOptions().transform(
                    CenterCrop(),
                    RoundCorners(0f, corner, corner, 0f)
                )
            ).into(imageView)
        }

        /**
         * 加载网络图片
         */
        fun load(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(RequestOptions.centerCropTransform()).into(imageView)
        }

        /**
         * 加载网络图片
         */
        fun load(imageView: ImageView, url: String, error: Int) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(RequestOptions.centerCropTransform().error(error).placeholder(error))
                .into(imageView)
        }

        /**
         * 加载网络图片
         */
        fun file(imageView: ImageView, path: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(path)
                .apply(RequestOptions.centerCropTransform()).into(imageView)
        }

        /**
         * 加载网络图片
         */
        fun loadCircleCrop(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(RequestOptions.circleCropTransform()).into(imageView)
        }


        /**
         * 加载资源图片
         */
        fun loadScaledWH(imageView: ImageView, url: String) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).asBitmap().load(url).apply(RequestOptions.centerCropTransform())
                .into(object : SimpleTarget<Bitmap>() {
                    override fun onResourceReady(
                        resource: Bitmap,
                        transition: Transition<in Bitmap>?
                    ) {
                        imageView.viewTreeObserver.addOnGlobalLayoutListener(object :
                            ViewTreeObserver.OnGlobalLayoutListener {
                            override fun onGlobalLayout() {
                                imageView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                                val c = resource.height.toFloat() / resource.width.toFloat()
                                imageView.layoutParams.height = (c * imageView.width).toInt()
                                Glide.with(imageView)
                                    .load(resource)
                                    .apply(RequestOptions.centerCropTransform())
                                    .into(imageView)
                            }
                        })
                    }

                })
        }


        /**
         * 加载网络图片
         */
        fun loadCorner(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, corner, corner, corner)
                    )
                ).into(imageView)
        }


        /**
         * 加载网络图片
         */
        fun loadCornerTop(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, corner, 0f, 0f)
                    )
                ).into(imageView)
        }


        /**
         * 加载网络图片
         */
        fun loadCornerLeft(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(corner, 0f, 0f, corner)
                    )
                ).into(imageView)
        }


        /**
         * 加载网络图片
         */
        fun loadCornerRight(imageView: ImageView, url: String, corner: Float) {
            if (imageViewContextDestroyed(imageView)) return
            Glide.with(imageView).load(AppContext.appContext.getImgBaseUrl() + url)
                .apply(
                    RequestOptions().transform(
                        CenterCrop(),
                        RoundCorners(0f, corner, corner, 0f)
                    )
                ).into(imageView)
        }
    }
}